import Mock from 'mockjs';
import { API_CODE } from '@/api/constant';

// 获取订单列表
Mock.mock(/\/api\/order\/list/, 'get', (options: any) => {
  const { page = 1, pageSize = 10 } = options.url.match(/page=(\d+)&pageSize=(\d+)/)
    ? {
        page: parseInt(options.url.match(/page=(\d+)/)[1]),
        pageSize: parseInt(options.url.match(/pageSize=(\d+)/)[1])
      }
    : { page: 1, pageSize: 10 };

  const list = [];
  for (let i = 0; i < pageSize; i++) {
    list.push(
      Mock.mock({
        id: '@guid',
        orderNo: /^ORDER\d{8}\w{8}$/,
        'status|1': ['pending', 'processing', 'completed', 'cancelled'],
        createTime: '@datetime',
        updateTime: '@datetime',
        'amount|100-10000.1-2': 100,
        'discount|0-50.1-2': 0,
        'tax|0-100.1-2': 0,
        'total|100-10000.1-2': 100,
        customer: {
          id: '@guid',
          name: '@cname',
          phone: /^1[3-9]\d{9}$/,
          email: '@email'
        },
        'products|1-5': [
          {
            id: '@guid',
            name: '@ctitle(5, 10)',
            'price|10-1000.1-2': 100,
            'quantity|1-10': 1
          }
        ]
      })
    );
  }

  return {
    code: API_CODE.SUCCESS,
    message: '获取订单列表成功',
    data: {
      list,
      total: 100,
      page,
      pageSize
    }
  };
});

// 获取订单详情
Mock.mock(/\/api\/order\/detail\/\w+/, 'get', (options: any) => {
  const id = options.url.match(/\/api\/order\/detail\/(\w+)/)[1];

  return {
    code: API_CODE.SUCCESS,
    message: '获取订单详情成功',
    data: Mock.mock({
      id,
      orderNo: /^ORDER\d{8}\w{8}$/,
      'status|1': ['pending', 'processing', 'completed', 'cancelled'],
      createTime: '@datetime',
      updateTime: '@datetime',
      'amount|100-10000.1-2': 100,
      'discount|0-50.1-2': 0,
      'tax|0-100.1-2': 0,
      'total|100-10000.1-2': 100,
      customer: {
        id: '@guid',
        name: '@cname',
        phone: /^1[3-9]\d{9}$/,
        email: '@email',
        address: '@county(true)'
      },
      'products|1-5': [
        {
          id: '@guid',
          name: '@ctitle(5, 10)',
          'price|10-1000.1-2': 100,
          'quantity|1-10': 1,
          'subtotal|10-10000.1-2': 100
        }
      ],
      'logs|1-10': [
        {
          id: '@guid',
          'action|1': ['create', 'update', 'pay', 'ship', 'deliver', 'complete', 'cancel'],
          operator: '@cname',
          time: '@datetime',
          remark: '@csentence'
        }
      ]
    })
  };
});

// 创建订单
Mock.mock(/\/api\/order\/create/, 'post', (options: any) => {
  const data = JSON.parse(options.body);

  return {
    code: API_CODE.SUCCESS,
    message: '创建订单成功',
    data: {
      id: Mock.Random.guid(),
      orderNo: `ORDER${Mock.Random.date('yyyyMMdd')}${Mock.Random.string('upper', 8)}`,
      ...data,
      status: 'pending',
      createTime: Mock.Random.datetime(),
      updateTime: Mock.Random.datetime()
    }
  };
});

// 更新订单
Mock.mock(/\/api\/order\/update\/\w+/, 'put', (options: any) => {
  const id = options.url.match(/\/api\/order\/update\/(\w+)/)[1];
  const data = JSON.parse(options.body);

  return {
    code: API_CODE.SUCCESS,
    message: '更新订单成功',
    data: {
      id,
      ...data,
      updateTime: Mock.Random.datetime()
    }
  };
});

// 删除订单
Mock.mock(/\/api\/order\/delete\/\w+/, 'delete', (options: any) => {
  const id = options.url.match(/\/api\/order\/delete\/(\w+)/)[1];

  return {
    code: API_CODE.SUCCESS,
    message: '删除订单成功',
    data: { id }
  };
});
